﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Xml;
using System.Xml.Serialization;

namespace PassBag.Logic.Settings {
    /// <summary>
    /// Contain application settingd
    /// </summary>
    [Serializable]
    public sealed class XmlSettings {
        #region constants
        [XmlIgnore]
        private const string DEFAULT_FILE_EXTENSION = "pbg";
        [XmlIgnore]
        private const string DEFAULT_BACKUP_FILE_EXTENSION = "bpb";
        #endregion

        #region properties holders
        // non serialized settings
        private const int MIN_PASSWORD_LENGTH = 5;
        // general
        private bool _generalUseSystemTray = true;
        private int _generalLockAfterXSecond = 60; // 0 if no lock
        private bool _generalLockOnMinimize = false;
        private bool _generalClearClipboardOnLock = true;
        private bool _generalStartAtLogin = false;
        private string _generalLanguage = string.Empty;

        // backup
        private string _backupDefaultOSX = string.Empty;
        private string _backupDefaultLinux = string.Empty;
        private string _backupDefaultWindows = string.Empty;
        // merge
        /*
        private string _mergeDefaultUsbInstallPath = string.Empty;
        private string _mergeDefaultOSXInstallPath = string.Empty;
        private string _mergeDefaultLinuxInstallPath = string.Empty;
        private string _mergeDefaultWindowsInstallPath = string.Empty;
        */
        // password policy
        private bool _passPolicyUseDefault = true;
        private int _passPolicyNumberOfDigit = 4;
        private int _passPolicyNumberOfSpecialChars = 2;
        private int _passPolicyMaxLength = 10;
        // hidden
        private string _lastOpenFile = string.Empty;
        private string _recentOpenFile1 = string.Empty;
        private string _recentOpenFile2 = string.Empty;
        private string _recentOpenFile3 = string.Empty;
        private string _recentOpenFile4 = string.Empty;
        private string _recentOpenFile5 = string.Empty; 
        #endregion

        #region serialized settings

        /// <summary>
        /// Gets or sets a value indicating whether the application use system tray.
        /// </summary>
        /// <remarks>
        /// This methode is not used on PassBag v 1
        /// </remarks>
        /// <value>
        /// 	<c>true</c> if [use system tray]; otherwise, <c>false</c>.
        /// </value>
        public bool GeneralUseSystemTray {
            get {
                return _generalUseSystemTray;
            }
            set {
                _generalUseSystemTray = value;
            }
        }

        /// <summary>
        /// Gets or sets the general lock after X second.
        /// </summary>
        /// <value>The general lock after X second.</value>
        public int GeneralLockAfterXSecond {
            get {
                return _generalLockAfterXSecond;
            }
            set {
                _generalLockAfterXSecond = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [general lock on minimize].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [general lock on minimize]; otherwise, <c>false</c>.
        /// </value>
        public bool GeneralLockOnMinimize {
            get {
                return _generalLockOnMinimize;
            }
            set {
                _generalLockOnMinimize = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [general clear clipboard on lock].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [general clear clipboard on lock]; otherwise, <c>false</c>.
        /// </value>
        public bool GeneralClearClipboardOnLock {
            get {
                return _generalClearClipboardOnLock;
            }
            set {
                _generalClearClipboardOnLock = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [general start at login].
        /// </summary>
        /// <remarks>
        /// This methode is not used on PassBag v 1
        /// </remarks>
        /// <value>
        /// 	<c>true</c> if [general start at login]; otherwise, <c>false</c>.
        /// </value>
        public bool GeneralStartAtLogin {
            get {
                return _generalStartAtLogin;
            }
            set {
                _generalStartAtLogin = value;
            }
        }

        /// <summary>
        /// Gets or sets the general language.
        /// </summary>
        /// <value>The general language.</value>
        public string GeneralLanguage {
            get {
                return _generalLanguage;
            }
            set {
                _generalLanguage = value;
            }
        } 
        
        /// <summary>
        /// Gets or sets the XML backup default OSX.
        /// </summary>
        /// <value>The XML backup default OSX.</value>
        public XmlCDataSection XmlBackupDefaultOSX
        {
            get {
                return GetCData(_backupDefaultOSX);
            }
            set {
                _backupDefaultOSX = (value as XmlCDataSection).Value;
            }
        }
        /// <summary>
        /// Gets or sets the XML backup default linux.
        /// </summary>
        /// <value>The XML backup default linux.</value>
        public XmlCDataSection XmlBackupDefaultLinux
        {
            get {
                return GetCData(_backupDefaultLinux);
            }
            set {
                _backupDefaultLinux = (value as XmlCDataSection).Value;
            }
        }

        /// <summary>
        /// Gets or sets the XML backup default windows.
        /// </summary>
        /// <value>The XML backup default windows.</value>
        public XmlCDataSection XmlBackupDefaultWindows
        {
            get {
                return GetCData(_backupDefaultWindows);
            }
            set {
                _backupDefaultWindows = (value as XmlCDataSection).Value;
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether [pass policy use default].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [pass policy use default]; otherwise, <c>false</c>.
        /// </value>
        public bool PassPolicyUseDefault {
            get {
                return _passPolicyUseDefault;
            }
            set {
                _passPolicyUseDefault = value;
            }
        }

        /// <summary>
        /// Gets or sets the pass policy number of digit.
        /// </summary>
        /// <value>The pass policy number of digit.</value>
        public int PassPolicyNumberOfDigit {
            get {
                return _passPolicyNumberOfDigit;
            }
            set {
                _passPolicyNumberOfDigit = value;
            }
        }

        /// <summary>
        /// Gets or sets the pass policy number of special chars.
        /// </summary>
        /// <value>The pass policy number of special chars.</value>
        public int PassPolicyNumberOfSpecialChars {
            get {
                return _passPolicyNumberOfSpecialChars;
            }
            set {
                _passPolicyNumberOfSpecialChars = value;
            }
        }

        /// <summary>
        /// Gets or sets the length of the pass policy max.
        /// </summary>
        /// <value>The length of the pass policy max.</value>
        public int PassPolicyMaxLength {
            get {
                return _passPolicyMaxLength;
            }
            set {
                _passPolicyMaxLength = value;
            }
        }
        #endregion


        #region recent open file list

        /// <summary>
        /// Gets or sets the recent open file1.
        /// </summary>
        /// <value>The recent open file1.</value>
        public string RecentOpenFile1 {
            get {
                return _recentOpenFile1;
            }
            set {
                _recentOpenFile1 = value;
            }
        }

        /// <summary>
        /// Gets or sets the recent open file2.
        /// </summary>
        /// <value>The recent open file2.</value>
        public string RecentOpenFile2 {
            get {
                return _recentOpenFile2;
            }
            set {
                _recentOpenFile2 = value;
            }
        }

        /// <summary>
        /// Gets or sets the recent open file3.
        /// </summary>
        /// <value>The recent open file3.</value>
        public string RecentOpenFile3 {
            get {
                return _recentOpenFile3;
            }
            set {
                _recentOpenFile3 = value;
            }
        }

        /// <summary>
        /// Gets or sets the recent open file4.
        /// </summary>
        /// <value>The recent open file4.</value>
        public string RecentOpenFile4 {
            get {
                return _recentOpenFile4;
            }
            set {
                _recentOpenFile4 = value;
            }
        }

        /// <summary>
        /// Gets or sets the recent open file5.
        /// </summary>
        /// <value>The recent open file5.</value>
        public string RecentOpenFile5 {
            get {
                return _recentOpenFile5;
            }
            set {
                _recentOpenFile5 = value;
            }
        } 
        #endregion

        #region no serialized settings
        [XmlIgnore]
        public int MinPasswordLength {
            get {
                return MIN_PASSWORD_LENGTH;
            }
        }

        [XmlIgnore]
        public string FileExtension {
            get {
                return DEFAULT_FILE_EXTENSION;
            }
        } 

        [XmlIgnore]
        public string BackupFileExtension {
            get {
                return DEFAULT_BACKUP_FILE_EXTENSION;
            }
        }

        [XmlIgnore]
        public string BackupDefaultOSX
        {
            get
            {
                return _backupDefaultOSX;
            }
            set
            {
                _backupDefaultOSX = value;
            }
        }

        [XmlIgnore]
        public string BackupDefaultLinux
        {
            get
            {
                return _backupDefaultLinux;
            }
            set
            {
                _backupDefaultLinux = value;
            }
        }

        [XmlIgnore]
        public string BackupDefaultWindows
        {
            get
            {
                // 200.01.23 - BUG0001 - remove debug directive, bug patched
                //#if DEBUG
                //return @"c:\temp\passbag." + DEFAULT_BACKUP_FILE_EXTENSION;
                //#else
                return _backupDefaultWindows;
                //#endif

            }
            set
            {
                _backupDefaultWindows = value;
            }
        }
           
        #endregion

        /// <summary>
        /// Help to serialize cdata section
        /// </summary>
        /// <param name="value">string value</param>
        /// <returns>CData value</returns>
        private XmlCDataSection GetCData(string value)
        {
            XmlDataDocument cdata = new XmlDataDocument();
            return cdata.CreateCDataSection(value);
        }

    }
}
